package com.mingzhi.permissionservice.controller;

import com.mingzhi.permissionservice.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    /**
     * 绑定默认角色
     *
     * @param userId
     */
    @PostMapping("/bindDefaultRole/{userId}")
    public Integer bindDefaultRole(@PathVariable("userId") Long userId) {
        return permissionService.bindDefaultRole(userId);
    }

    /**
     * 获取用户角色码
     */
    @PostMapping("/getRoleCode/{userId}")
    public String getUserRoleCode(@PathVariable("userId") Long userId) {
        return permissionService.getUserRoleCode(userId);
    }

    /**
     * 升级为管理员
     */
    @GetMapping("/upgradeToAdmin")
    public void upgradeToAdmin(@RequestParam String userId) {
        log.info("升级为管理员");
        try {
            // 使用BigDecimal处理大整数
            BigDecimal id = new BigDecimal(userId);
            permissionService.upgradeToAdmin(id.longValue());
        } catch (NumberFormatException e) {
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid user ID format");
        }

    }

    /**
     * 降级为普通用户
     */
    @GetMapping("/downgradeToUser")
    public void downgradeToUser(@RequestParam String userId) {
        log.info("降级为普通用户");
        try {
            // 使用BigDecimal处理大整数
            BigDecimal id = new BigDecimal(userId);
            permissionService.downgradeToUser(id.longValue());
        } catch (NumberFormatException e) {
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid user ID format");
        }

    }

    /**
     * 获取所有普通用户id
     *
     * @param
     * @return
     */
    @GetMapping("/getNormalUserIds")
    public List<Long> getNormalUserIds() {
        return permissionService.getNormalUserIds();
    }

}
